import { BASE_PATH } from "@/config";

export class Loader {
  constructor(loader, config) {
    this.loader = loader;
    this.config = config;
    this.resources = {};
  }

  preload() {
    this.loader.add(`ball`, `${BASE_PATH}/img/game/game-ball.png`)
    this.loader.add(`bg_left`, `${BASE_PATH}/img/game/game-bg-left.jpg`)
    this.loader.add(`bg_right`, `${BASE_PATH}/img/game/game-bg-right.jpg`)
    this.loader.add(`basket_board`, `${BASE_PATH}/img/sprites/basket_board_left.png`)
    this.loader.add(`basket_net`, `${BASE_PATH}/img/sprites/basket_net_left.png`)
    this.loader.add(`basket_rim`, `${BASE_PATH}/img/sprites/basket_rim_left.png`)
    this.loader.add(`timer`, `${BASE_PATH}/img/game/game-timer.png`)

    this.loader.add(`basket_board_right`, `${BASE_PATH}/img/sprites/basket_board_right.png`)
    this.loader.add(`basket_net_right`, `${BASE_PATH}/img/sprites/basket_net_right.png`)
    this.loader.add(`basket_rim_right`, `${BASE_PATH}/img/sprites/basket_rim_right.png`)


    for (let i = 1; i <= 11; i++) {
      this.loader.add(`net-${i}`, `${BASE_PATH}/img/game/frame/k-${i}.png`);
    }
    for (let i = 1; i <= 11; i++) {
      this.loader.add(`keyf-${i}`, `${BASE_PATH}/img/game/test/keyf-${i}.png`);
    }
    for (let i = 1; i <= 7; i++) {
      this.loader.add(`ball-f-${i}`, `${BASE_PATH}/img/game/bframe/b-${i}.png`);
    }


    this.loader.add(`bg`, `${BASE_PATH}/img/game/game-bg.jpg`)
    this.loader.add(`diamond`, `${BASE_PATH}/img/sprites/diamond.png`)
    this.loader.add(`hero`, `${BASE_PATH}/img/sprites/hero.png`)
    this.loader.add(`jump`, `${BASE_PATH}/img/sprites/jump.png`)
    this.loader.add(`platform`, `${BASE_PATH}/img/sprites/platform.png`)
    this.loader.add(`tile`, `${BASE_PATH}/img/sprites/tile.png`)
    this.loader.add(`walk1`, `${BASE_PATH}/img/sprites/walk1.png`)
    this.loader.add(`walk2`, `${BASE_PATH}/img/sprites/walk2.png`)


    // for (const asset of this.config.loader) {
    //   let key = asset.key.substr(asset.key.lastIndexOf('/') + 1);
    //   key = key.substring(0, key.indexOf('.'));
    //   console.log(`key`, key, asset.data.default)
    //   if (asset.key.indexOf(".png") !== -1 || asset.key.indexOf(".jpg") !== -1) {
    //
    //     if (!this.loader.resources.hasOwnProperty(key)) this.loader.add(key, asset.data.default)
    //   }
    // }

    return new Promise(resolve => {
      this.loader.load((loader, resources) => {
        this.resources = resources;
        resolve();
      });
    });
  }
}